package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.converter.SmsQuotaConverter;
import com.aliyun.messagemgt.dto.SmsQuota;
import com.aliyun.messagemgt.dto.vo.SmsQuotaRequestVO;
import com.aliyun.messagemgt.dto.vo.SmsQuotaResponseVO;
import com.aliyun.messagemgt.domain.repository.SmsQuotaRepository;
import com.aliyun.messagemgt.entities.SmsQuotaEntity;
import com.aliyun.messagemgt.entities.SmsQuotaMsgSourceEntity;
import com.aliyun.messagemgt.repository.mappers.SmsQuotaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author hzw
 * @Date 2020/12/15 15:21
 * @Version 1.0
 */
@Service
public class SmsQuotaMybatisRepository implements SmsQuotaRepository {
    @Autowired
    private SmsQuotaMapper smsQuotaMapper;

    /**
     * 插入 短信配额数据
     *
     * @param smsQuota 短信配额类
     * @return int
     */
    @Override
    public int insert(SmsQuota smsQuota) {

        return smsQuotaMapper.insert(smsQuota);
    }

    /**
     * 更新短信配额
     *
     * @param smsQuota 短信配额类
     * @return int
     */
    @Override
    public int update(SmsQuota smsQuota) {
        return smsQuotaMapper.update(smsQuota);
    }

    /**
     * 查询短信配额列表
     *
     * @param smsQuotaRequestVO 查询类
     * @return list
     */
    @Override
    public List<SmsQuotaResponseVO> querySmsQuotaList(SmsQuotaRequestVO smsQuotaRequestVO) {
        List<SmsQuotaMsgSourceEntity> smsQuotaMsgSourceEntityList = smsQuotaMapper.querySmsQuotaList(smsQuotaRequestVO);
        return SmsQuotaConverter.INSTANCE.smsQuotaMsgSourceEntityList2smsQuotaResponseVOList(smsQuotaMsgSourceEntityList);
    }

    /**
     * 根据消息源id和生效状态查询短信配额
     *
     * @param msgSourceId 消息源UUID
     * @param status      生效状态
     * @return smsQuota
     */
    @Override
    public SmsQuota queryQuotaByMsgSourceIdAndStatus(String msgSourceId, Integer status) {
        SmsQuotaEntity smsQuotaEntity = smsQuotaMapper.queryQuotaByMsgSourceIdAndStatus(msgSourceId, status);
        SmsQuota smsQuota = SmsQuotaConverter.INSTANCE.smsQuotaEntity2smsQuota(smsQuotaEntity);
        return smsQuota;
    }

    /**
     * 根据生效状态查询短信配额列表
     *
     * @param status 生效状态
     * @return list
     */
    @Override
    public List<SmsQuota> queryQuotaByStatus(Integer status) {
        List<SmsQuotaEntity> smsQuotaEntityList = smsQuotaMapper.queryQuotaByStatus(status);
        List<SmsQuota> smsQuotaList = SmsQuotaConverter.INSTANCE.smsQuotaEntityList2smsQuotaList(smsQuotaEntityList);
        return smsQuotaList;
    }
}
